/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Administration;

import pojo.hibernate.Departmentmaster;
import pojo.hibernate.DepartmentmasterDAO;
import pojo.hibernate.Institutionmaster;
import pojo.hibernate.InstitutionmasterDAO;
import pojo.hibernate.Subinstitutionmaster;
import pojo.hibernate.SubinstitutionmasterDAO;
import pojo.hibernate.Statemaster;
import pojo.hibernate.StatemasterDAO;

import utils.DevelopmentSupport;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author kazim
 */
public class DepartmentAction extends DevelopmentSupport {

    private Departmentmaster dm;
    private List<Departmentmaster> dmList = new ArrayList<Departmentmaster>();
    private List<Institutionmaster> imIdList = new ArrayList<Institutionmaster>();
    private List<Subinstitutionmaster> simImIdList = new ArrayList<Subinstitutionmaster>();
    private List<Statemaster> statemasterList = new ArrayList<Statemaster>();
    private DepartmentmasterDAO dmDao = new DepartmentmasterDAO();
    private InstitutionmasterDAO imDao = new InstitutionmasterDAO();
    private SubinstitutionmasterDAO simDao = new SubinstitutionmasterDAO();
    private StatemasterDAO statemasterDao = new StatemasterDAO();
    private String message;
    private Integer dmId;

    public void setdmId(Integer dmId) {
        this.dmId = dmId;
    }

    public Integer getdmId() {
        return this.dmId;
    }    

    public void setimIdList(List<Institutionmaster> imIdList) {
        this.imIdList = imIdList;
    }

    public List<Institutionmaster> getimIdList() {
        return this.imIdList;
    }

    public void setsimImIdList(List<Subinstitutionmaster> simImIdList) {
        this.simImIdList = simImIdList;
    }

    public List<Subinstitutionmaster> getsimImIdList() {
        return this.simImIdList;
    }

    public void setstatemasterList(List<Statemaster> statemasterList) {
        this.statemasterList = statemasterList;
    }

    public List<Statemaster> getstatemasterList() {
        return this.statemasterList;
    }

    public void setdm(Departmentmaster dm) {
        this.dm = dm;
    }

    public Departmentmaster getdm() {
        return this.dm;
    }

    public void setdmList(List<Departmentmaster> dmList) {
        this.dmList = dmList;
    }

    public List<Departmentmaster> getdmList() {
        return this.dmList;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return this.message;
    }

    @Override
    public String execute() throws Exception {
        try {
            imIdList = imDao.findAll();
            simImIdList = simDao.findAll();
            statemasterList = statemasterDao.findAll();

            return SUCCESS;
        } catch (Exception e) {
            message = "Exception in -> DepartmentAxn"  + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }

    public String Browse() throws Exception {
        try {
            dmList = dmDao.findAll();
            return SUCCESS;
        } catch (Exception e) {
            message = "Exception in Browse method -> DepartmentAxn" + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }

    public String Save() throws Exception {
        try {
            //If part saves record for the first time; else parts is for record update
            if (dm.getDmId() == null) {
                dmDao.save(dm);
                message = "Department record saved successfully. Departmenyt Id is " + dm.getDmId();
            } else {
                Departmentmaster dm2 = dmDao.findByDmId(dm.getDmId());
                dm2 = dm;
                dmDao.update(dm2);
                message = "Department record saved successfully.";
            }
            dmList = dmDao.findAll();
            return SUCCESS;
        } catch (Exception e) {
            message = "Exception in Save method -> DepartmentAxn" + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;

        }
    }

    public String Edit() throws Exception {
        try {
            imIdList = imDao.findAll();
            simImIdList = simDao.findAll();
            statemasterList = statemasterDao.findAll();
            dm = dmDao.findByDmId(getdmId());
            return SUCCESS;
        } catch (Exception e) {
            message = "Exception in Edit method -> DepartmentAxn" + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }

    public String Delete() throws Exception {
        try {
            dm = dmDao.findByDmId(getdmId());
            dmDao.delete(dm);
            dmList = dmDao.findAll();
            return SUCCESS;
        } catch (Exception e) {
            message = "Exception in Delete method -> DepartmentAxn " + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }

    public String Clear() throws Exception {
        try {

            dm = null;
            
            imIdList = imDao.findAll();
            simImIdList = simDao.findAll();
            statemasterList = statemasterDao.findAll();

            return SUCCESS;
        } catch (Exception e) {
            message = "Exception in -> DepartmentAxn"  + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }


}
